package syntaxcolor.v4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import syntaxcolor.v4.syntaxcolor.CodeStyleText;
import syntaxcolor.v4.syntaxcolor.RuleFactory;
import syntaxcolor.v4.syntaxcolor.rule.IRule;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class TestDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	Display display = Display.getDefault();

	public final Color JAVADOC_DEFAULT = new Color(display, 98, 175, 244); // @jve:decl-index=0:
	public final Color SINGLE_LINE_COMMENT = new Color(display, 0, 255, 0);
	public final Color KEYWORD = new Color(display, 128, 0, 128); // @jve:decl-index=0:
	public final Color STRING = new Color(display, 0, 0, 255); // @jve:decl-index=0:
	private CodeStyleText styledText;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public TestDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			try {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE
				| SWT.APPLICATION_MODAL);
		shell.setSize(575, 387);
		shell.setText(getText());
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		styledText = new CodeStyleText(shell, SWT.BORDER
				| SWT.H_SCROLL | SWT.V_SCROLL);

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				testSearchRules();
			}
		});
		btnNewButton.setText("New Button");
		styledText.addRule(buildJavaRules());
	}

	protected void testSearchRules() {
		styledText.getStyleRangeByRule("placeSign");
	}

	public static void main(String[] args) {
		TestDialog dlg = new TestDialog(new Shell(), SWT.NONE);
		dlg.open();
	}

	private IRule[] buildJavaRules() {

		StyleRange javaDocStyle = new StyleRange();
		javaDocStyle.foreground = JAVADOC_DEFAULT;

		StyleRange commentStyle = new StyleRange();
		commentStyle.foreground = SINGLE_LINE_COMMENT;

		StyleRange keywordStyle = new StyleRange();
		keywordStyle.foreground = KEYWORD;
		keywordStyle.font = new Font(display, "宋体", 9, SWT.BOLD);

		StyleRange placeStyle = new StyleRange();
		placeStyle.foreground = Display.getDefault().getSystemColor(
				SWT.COLOR_RED);
		placeStyle.font = new Font(display, "宋体", 9, SWT.BOLD);
		placeStyle.underline = true;
		StyleRange stringStyle = new StyleRange();
		stringStyle.foreground = STRING;

		return new IRule[] {
				RuleFactory.createMultiLineRule("comment", "/*", "*/",
						commentStyle),
				RuleFactory.createSingleLineRule("string", "\"", "\"", '\\',
						stringStyle),
				RuleFactory.createSingleLineRule("string", "\'", "\'", '\\',
						stringStyle),
				RuleFactory.createSingleLineRule("comment", "--", commentStyle),
				RuleFactory.createKeywordRule("keyword", false, keywordStyle,
						new String[] { "select", "insert", "delete", "left",
								"join", "if", "ifelse", "right", "is", "as",
								"of", "where", "inner", "and", "like", "or",
								"union", "all", "from" }),
				RuleFactory.createKeywordRule("placeSign", false, placeStyle,
						new String[] { "?" })

		};
	}
}
